﻿//--------------------------------------------------
// Motion Framework
// Copyright©2018-2020 何冠峰
// Licensed under the MIT license
//--------------------------------------------------
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using MotionFramework.Reference;

namespace MotionFramework.Console
{
    [ConsoleAttribute("日志过滤", 102)]
    internal class GameFilterLogWindow : IConsoleWindow
    {
        private class LogWrapper : IReference
        {
            public GameLogType GameLogType;
            public LogType Type;
            public string Log;
            public void OnRelease()
            {
                Log = string.Empty;
            }
        }

        /// <summary>
        /// 日志最大显示数量
        /// </summary>
        private const int LOG_MAX_COUNT = 500;

        /// <summary>
        /// 日志集合
        /// </summary>
        private List<LogWrapper> _logs = new List<LogWrapper>();

        /// <summary>
        /// 是否开启关闭
        /// </summary>
        private Dictionary<GameLogType, bool> _stateDic = new Dictionary<GameLogType, bool>();

        // GUI相关
        private Vector2 _scrollPos = Vector2.zero;

        public GameFilterLogWindow()
        {
            // 注册UnityEngine日志系统
            Application.logMessageReceived += HandleUnityEngineLog;

            GameLogType[] temp = (GameLogType[])Enum.GetValues(typeof(GameLogType));
            for (int i = 0; i < temp.Length; i++)
            {
                _stateDic.Add(temp[i], false);
            }
        }
        void IConsoleWindow.OnGUI()
        {
            GUILayout.BeginHorizontal();
            Dictionary<GameLogType, bool> tempDic = new Dictionary<GameLogType, bool>(_stateDic);
            foreach (var item in _stateDic)
            {
                tempDic[item.Key] = ConsoleGUI.Toggle(item.Key.ToString(), item.Value);
            }
            _stateDic = tempDic;
            GUILayout.EndHorizontal();

            float offset = ConsoleGUI.ToolbarStyle.fixedHeight;
            _scrollPos = ConsoleGUI.BeginScrollView(_scrollPos, offset);

            for (int i = 0; i < _logs.Count; i++)
            {
                if (_stateDic[_logs[i].GameLogType]) 
                {
                    ConsoleGUI.Lable(_logs[i].Log);
                }
            }

            ConsoleGUI.EndScrollView();
        }

        private void HandleUnityEngineLog(string logString, string stackTrace, LogType type)
        {
            LogWrapper wrapper = ReferencePool.Spawn<LogWrapper>();
            wrapper.Type = type;

            if (type == LogType.Assert || type == LogType.Error || type == LogType.Exception)
                wrapper.Log = logString + "\n" + stackTrace;
            else
                wrapper.Log = logString;


            Dictionary<GameLogType, bool> tempDic = new Dictionary<GameLogType, bool>(_stateDic);
            foreach (var item in _stateDic)
            {
                if (logString.StartsWith($"[{item.Key.ToString()}]"))
                {
                    wrapper.GameLogType = item.Key;
                    _logs.Add(wrapper);

                    if (_logs.Count > LOG_MAX_COUNT)
                    {
                        ReferencePool.Release(_logs[0]);
                        _logs.RemoveAt(0);
                    }
                }
            }
    
        }

    
    }

    public enum GameLogType
    {
        GTA = 0,
        ADS = 1,
        CountlyLog = 2,
        FBLog = 3,
        FireLog = 4,
        IAP = 5,
    }
}